<template>
  <div>
    <div class="node" v-has="{action: 'level:list', effect: 'hide'}">
      <Panel title="用户等级使用限制">
        <div slot='ext' :class="$style.examine">
          <el-button v-has="{action: 'level:add', effect: 'move'}" type="success" icon="el-icon-plus" @click="()=>editStore()">新增</el-button>
      </div>
      <TableEncapsulation
        slot="body"
        :tableConfig="tableConfig"
        :tableData="tableData"
      >
      <template slot="operation" slot-scope="operation">
            <el-button
              v-has="{action: 'level:update', effect: 'move'}"
              type="text"
              @click="()=>editStore(operation.row)"
            >编辑</el-button>
            <el-button
              v-has="{action: 'level:delete', effect: 'move'}"
              type="text"
              @click="()=>remove(operation.row.id)"
            >删除</el-button>
          </template>
          <TablePager
            slot="pager"
            v-if="pager.use"
            :size="pager.size"
            :pageChange="pageChange"
            :show-total="pager.showTotal"
            :total="pager.total"
            :current="pager.current"
          >
            <span class="demonstration">GO></span>
          </TablePager>
      </TableEncapsulation>
      </Panel>
      
      <!-- 编辑、新增 -->
      <ModalPanel 
        :visible='visible'
        title="编辑"
        :onCancel="onCancel"
        :class="$style.bannerDialog"
        :onOk="onSubmit">
        <div>
          <ModifyForm
            :hiddenBtn='true'
            :editColumn='editColumn'
            :updateData="updateData"
            labelWidth='100px'
            ref="configModal"
            :class="$style.ModifyForm"
            :temporaryData='modalData'>
          </ModifyForm>
        </div>
      </ModalPanel>
      <!-- end -->
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'level:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array";
import ModifyForm from "@/components/ModifyForm";
import ModalPanel from "@/components/ModalPanel";
export default {
  name: "comboVipIndex",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput,
    ModifyForm,
    ModalPanel,
  },
  data() {
    return {
      tableData: [],
      query: {
        page: 1
      },
      ruleMountValue:'',
      pager: {
        component: 'comboVipIndex',
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      operation: true,
      searchs:{
        component: 'comboVipIndex',
        state: '',
        name: '',
      },
      // 编辑新增
      visible: false,
      modalData:{},
      typeData:[{'id': '1','name': "自营"},{'id':'2', 'name':"第三方"},{'id':'3', 'name':"租用"},{'id':'9', 'name':"私人"}],
      editColumn: [
        {
          name: "可用站点类型",
          prop: "siteTypes",
          type: "select",
          optionBindLabel: 'name',
          optionBindValue: 'id',
          optionList: [{'id': '1','name': "自营"},{'id':'2', 'name':"第三方"},{'id':'3', 'name':"租用"},{'id':'9', 'name':"私人"}],
          onChange: () => {},
          width:24,
          multiple: true,
          required: true
        },
      ],
    };
  },
  created() {
    this.getRate();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "编号",
            type: "text",
            prop: "id",
            align: "center"
          },
          {
            label: "站点类型",
            type: "text",
            prop: "siteTypeNames",
            align: "center"
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:180
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getRate() {
      let page = this.pager.current
      let search = objectFilter({ page })
      const res = await request({
        url: "/level/list/limit",
        method: "POST",
        data: search
      });
      this.tableData = res.list
      this.pager.total = 1
      this.pager.current = 1
    },
    pageChange(val) {
      this.pager.current = val;
      this.getRate();
    },
    changeSearch(key, val) {
      this.searchs[key] = val;
      this.query.page = 1;
    },
    band(row){
      this.$router.push({ path: "/combo/vip/band",query:{id:row.id,name: row.name} })
    },
    //新增编辑start---
    editStore(rowData){
      if(rowData &&rowData.siteTypes){
        rowData.siteTypes = rowData.siteTypes.split("|");
      }
      console.log(rowData)
      this.modalData = {...rowData}
      this.visible = true
    },
    async onSubmit(){
      this.$refs['configModal'].onSubmit()
    },
    onCancel(){
      this.visible = false
    },
    async updateData(values) {
      var {id ,siteTypes} = values
      let url, msg
      if(id){
          url = '/level/update/limit'
          msg = '编辑成功'
      }else{
          url = '/level/add/limit'
          msg = '添加成功'
      }
      let typeData = this.typeData
      if(siteTypes.length>0){
        let newdata = typeData.filter(item =>siteTypes.indexOf(item.id) !=-1).map(item => {return item.name})
        var siteTypeNames =newdata.join(',')
        siteTypes = siteTypes.join('|')
      }else{
        this.$$message.error('请选择类型')
        return
      }
      console.log({id ,siteTypes})
      let res = await request({
        url: url,
        method: "POST",
        data: {id ,siteTypes, siteTypeNames}
      });
      if(res.code !==0 ) return
      this.$message.success(msg)
      this.visible = false
      this.getRate()
    },
    // 新增编辑end---
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        const del = await request({
        url: "/level/delete/limit",
        dataType: 'json',
        method: "POST",
        data: [id]
      });
        if(del.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getRate()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>